/**
* @file client_net.h
* @brief tcp客户端
* @version 1.0.0
* @date 2016-03-17
*/
#ifndef WH_COMMON_NET_CLIENT_BASE_H_
#define WH_COMMON_NET_CLIENT_BASE_H_

#include <stdint.h>
#include "channel_base.h"
#include "net_event_manager.h"
#include "timer.h"

#ifdef _MSC_VER
#include "winsocket.h"
#endif

namespace common
{
namespace net
{
/**
* @brief tcp客户端
*/

#ifdef _MSC_VER
	class CClientNet :
		public CTimer,
		public CChannelBase,
		public INetEventHandler
	{
	public:
		/**
		* @brief 构造函数
		*/
		CClientNet();

		/**
		* @brief 析构函数
		*/
		virtual ~CClientNet();


		int32_t Initialize(uint32_t receive_buffer_capacity, uint32_t send_buffer_capacity);

		int32_t Finalize();

		virtual void OnNetEvent(int fd, uint32_t events);
		/**
		* @brief 同步阻塞连接服务器(考虑有没有必要提供异步)
		*
		* @param address 服务器地址
		* @param port 端口
		*
		* @return 返回连接结果
		*/
		int32_t Connect(const char* address, uint16_t port);

		int32_t Disconnect();

		bool connected() const
		{
			return connected_;
		}

		int EnableReconnect(int32_t interval);

		int DisableReconnect();

		int CheckConnect();

		void TimerActive();

	protected:
		bool connected_;

		uint32_t send_buffer_size_;/**<发送缓存大小  */

		uint32_t receive_buffer_size_;/**<接收缓存大小  */

		SocketClient client_socket_;
	};
#else
class CClientNet :
    public CTimer,
    public CChannelBase,
    public INetEventHandler
{
public:

    /**
    * @brief 构造函数
    */
    CClientNet();

    /**
    * @brief 析构函数
    */
    virtual ~CClientNet();


    int32_t Initialize(uint32_t receive_buffer_capacity, uint32_t send_buffer_capacity);

    int32_t Finalize();

    virtual void OnNetEvent(int fd, uint32_t events);
    /**
    * @brief 同步阻塞连接服务器(考虑有没有必要提供异步)
    *
    * @param address 服务器地址
    * @param port 端口
    *
    * @return 返回连接结果
    */
    int32_t Connect(const char* address, uint16_t port);

    int32_t Disconnect();

    bool connected() const
    {
        return connected_;
    }

    int EnableReconnect(int32_t interval);

    int DisableReconnect();

    int CheckConnect();

    void TimerActive();

protected:
    bool connected_;

    uint32_t send_buffer_size_;/**<发送缓存大小  */

    uint32_t receive_buffer_size_;/**<接收缓存大小  */
};
#endif
}
}

#endif//WH_COMMON_NET_CLIENT_BASE_H_
